package com.itheima.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.WebVisit;
import com.itheima.pojo.WebVisitVO;
import com.itheima.service.WebVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/webVisit")
public class WebVisitController {

    @Autowired
    private WebVisitService webVisitService;

    @PostMapping("/getList")
    public Map<String,Object> getList(@RequestBody WebVisitVO webVisitVO){

        Page<WebVisit> page = webVisitService.getList(webVisitVO);
        //返回结果集合 map
        Map<String,Object> result = new HashMap<>();
        //数据封装集合 data
        Map<String,Object> recordsMap = new HashMap<>();
        //替换查询到的数据集合
        List<Map<String,Object>> mapList = new ArrayList<>();

        List<WebVisit> webVisits = page.getResult();
        for (WebVisit webVisit : webVisits) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HashMap<String,Object> map = new HashMap<>();
            map.put("ip",webVisit.getIp());
            map.put("os",webVisit.getOs());
            map.put("ipSource",webVisit.getIpSource());
            map.put("behavior",webVisit.getBehavior());
            map.put("browser",webVisit.getBrowser());
            map.put("createTime",simpleDateFormat.format(webVisit.getCreateTime()));
            map.put("updateTime",simpleDateFormat.format(webVisit.getUpdateTime()));
            map.put("moduleUid",webVisit.getModuleUid());
            map.put("otherData",webVisit.getOtherData());
            map.put("status",webVisit.getStatus());
            map.put("uid",webVisit.getUid());
            map.put("userUid",webVisit.getUserUid());
            map.put("behaviorContent",webVisit.getOtherData());
            mapList.add(map);
        }
        recordsMap.put("records",mapList);
        recordsMap.put("total",page.getTotal());
        recordsMap.put("current",webVisitVO.getCurrentPage());
        recordsMap.put("size",webVisitVO.getPageSize());
        recordsMap.put("orders",null);
        recordsMap.put("optimizeCountSql",true);
        recordsMap.put("isSearchCount",true);

        result.put("code","success");
        result.put("data",recordsMap);
        //返回结果
        return result;
    }

}
